package com.waypolice.common.sms;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 描述:
 *
 * @author zyp
 * @description 发送中国网建短信
 * @create 2018-09-04 10:49
 */
public class SendMsgWebChinese {

    private static Logger logger = LoggerFactory.getLogger(SendMsgWebChinese.class);

    private static String webUrl;
    private static String uid;
    private static String Key;

    static{
        webUrl = "http://sms.webchinese.cn/web_api/";
        uid = "innomind";
        Key = "innomind87669951";
    }

    /**
     * @author:zyp
     * @description 发送短信
     * @date: 2018/9/4 10:53
     * @return
     */
    public static void sendSMS(String phoneNum,String content){
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(webUrl);
        // 在头文件中设置转码
        post.addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded;charset=gbk");
        NameValuePair[] data = { new NameValuePair("Uid", uid),
                new NameValuePair("Key", Key),
                //①、多个手机号请用半角,隔开如：13888888886,13888888887,1388888888 一次最多对100个手机发送。
                //  ②、短信内容支持长短信，最多300个字，普通短信66个字/条，长短信64个字/条计费
                new NameValuePair("smsMob", phoneNum),
                new NameValuePair("smsText", content) };
        post.setRequestBody(data);
        try {
            client.executeMethod(post);
            Header[] headers = post.getResponseHeaders();
            int statusCode = post.getStatusCode();
            String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
            post.releaseConnection();
        } catch (IOException e) {
            logger.error("inno...发送短信失败");
        }
    }
}